package org.xzx.shop_api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xzx.shop_api.entity.User;
import org.xzx.shop_api.mapper.UserMapper;
import org.xzx.shop_api.service.UserService;
import org.xzx.shop_api.utils.JsonResult;
import org.xzx.shop_api.utils.JwtUtils;
import org.xzx.shop_api.utils.Md5Utils;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public String login(String username, String password) {
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }
        String encrypt = Md5Utils.encrypt(password);
        if (encrypt.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        return  JwtUtils.generateToken(username);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }
}
